package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.*;
import cn.e4j.modular.crm.mapper.GwaeTMapper;
import cn.e4j.modular.crm.service.GwaeTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 销售渠道 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-01-11
 */
@Service
public class GwaeTServiceImpl extends ServiceImpl<GwaeTMapper, GwaeT> implements GwaeTService {

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwaelist(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwaeDtos = this.baseMapper.listGwaePage(page, dto);

        return gwaeDtos;
    }

    @Transactional(readOnly = true)
    public GwaeT getByGwae001(String ent, String gwae001) {
        UpdateWrapper<GwaeT> gwaeTUpdateWrapper = new UpdateWrapper<>();
        gwaeTUpdateWrapper.eq("GWAEENT", ent);
        gwaeTUpdateWrapper.eq("GWAE001", gwae001);
        GwaeT gwae = this.baseMapper.selectOne(gwaeTUpdateWrapper);
        return gwae;
    }

    @Transactional
    @Override
    public void add(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwaeT gwae = new GwaeT();
        gwae.setGWAE001(dto.getString("GWAE001"));
        gwae.setGWAEENT(shiroUser.getDBC());
        gwae.setGWAEACTI(dto.getString("GWAEACTI"));
        gwae.setGWAE002(" ");
        gwae.setGWAE003(" ");//预留栏位2
        gwae.setGWAEUSER(shiroUser.getACCOUNT());
        gwae.setGWAEGRUP(shiroUser.getORG_ID());
        gwae.setGWAEDATE(LocalDate.now());
        gwae.setGWAETIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwae.setGWAEORIG(shiroUser.getORG_ID());
        gwae.setGWAEORIU(shiroUser.getACCOUNT());
        gwae.setGWAEMODU(shiroUser.getACCOUNT());
        GwaelT gwael = new GwaelT();
        gwael.setGWAELENT(shiroUser.getDBC());
        gwael.setGWAEL001(dto.getString("GWAE001"));
        gwael.setGWAEL002(LangUtils.getLang(shiroUser.getLANG()));
        gwael.setGWAEL003(dto.getString("GWAEL003"));
        gwael.setGWAEL004(dto.getString("GWAEL004"));
        gwae.insert();
        gwael.insert();
       /* GwbwT gwbwT = new GwbwT();
        gwbwT.setGWBWENT(shiroUser.getDBC());
        gwbwT.setGWBW001(dto.getString("GWAE001"));
        gwbwT.setGWBW002(" ");
        gwbwT.setGWBW003(" ");
        gwbwT.setGWBWACTI(dto.getString("GWAEACTI"));
        gwbwT.insert();
        GwbwlT gwbwlT = new GwbwlT();
        gwbwlT.setGWBWLENT(shiroUser.getDBC());
        gwbwlT.setGWBWL001(dto.getString("GWAE001"));
        gwbwlT.setGWBWL002(LangUtils.getLang(shiroUser.getLANG()));
        gwbwlT.setGWBWL003(dto.getString("GWAEL003"));
        gwbwlT.setGWBWL004(dto.getString("GWAEL004"));
        gwbwlT.insert();
        GwagT gwagT = new GwagT();
        gwagT.setGWAGENT(shiroUser.getDBC());
        gwagT.setGWAG001(dto.getString("GWAE001"));
        gwagT.setGWAG002(dto.getString("GWAE001"));
        gwagT.setGWAG003(" ");
        gwagT.setGWAGACTI(dto.getString("GWAEACTI"));
        gwagT.insert();
        GwaglT gwaglT = new GwaglT();
        gwaglT.setGWAGLENT(shiroUser.getDBC());
        gwaglT.setGWAGL001(dto.getString("GWAE001"));
        gwaglT.setGWAGL002(LangUtils.getLang(shiroUser.getLANG()));
        gwaglT.setGWAGL003(dto.getString("GWAEL003"));
        gwaglT.setGWAGL004(dto.getString("GWAEL004"));
        gwaglT.insert();*/
    }

    @Transactional
    @Override
    public void edit(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwaeT gwae = new GwaeT();
        gwae.setGWAEACTI(dto.getString("GWAEACTI"));
        gwae.setGWAE002(dto.getString("GWAE002"));
        gwae.setGWAEMODU(shiroUser.getACCOUNT());
        UpdateWrapper<GwaeT> gwaeTUpdateWrapper = new UpdateWrapper<>();
        gwaeTUpdateWrapper.eq("GWAEENT", shiroUser.getDBC());
        gwaeTUpdateWrapper.eq("GWAE001", dto.getString("GWAE001"));

        GwaelT gwael = new GwaelT();
        gwael.setGWAEL003(dto.getString("GWAEL003"));
        gwael.setGWAEL004(dto.getString("GWAEL004"));
        UpdateWrapper<GwaelT> gwaelTUpdateWrapper = new UpdateWrapper<>();
        gwaelTUpdateWrapper.eq("GWAELENT", shiroUser.getDBC());
        gwaelTUpdateWrapper.eq("GWAEL001", dto.getString("GWAE001"));

        /*GwbwT gwbwT = new GwbwT();
        gwbwT.setGWBWACTI(dto.getString("GWAEACTI"));
        UpdateWrapper<GwbwT> gwbwTUpdateWrapper = new UpdateWrapper<>();
        gwbwTUpdateWrapper.eq("GWBWENT", shiroUser.getDBC());
        gwbwTUpdateWrapper.eq("GWBW001", dto.getString("GWAE001"));

        GwbwlT gwbwl = new GwbwlT();
        gwbwl.setGWBWL003(dto.getString("GWAEL003"));
        gwbwl.setGWBWL004(dto.getString("GWAEL004"));
        UpdateWrapper<GwbwlT> gwbwlTUpdateWrapper = new UpdateWrapper<>();
        gwbwlTUpdateWrapper.eq("GWBWLENT", shiroUser.getDBC());
        gwbwlTUpdateWrapper.eq("GWBWL001", dto.getString("GWAE001"));

        GwagT gwagT = new GwagT();
        gwagT.setGWAGACTI(dto.getString("GWAEACTI"));
        UpdateWrapper<GwagT> gwagTUpdateWrapper = new UpdateWrapper<>();
        gwagTUpdateWrapper.eq("GWAGENT", shiroUser.getDBC());
        gwagTUpdateWrapper.eq("GWAG001", dto.getString("GWAE001"));

        GwaglT gwagl = new GwaglT();
        gwagl.setGWAGL003(dto.getString("GWAEL003"));
        gwagl.setGWAGL004(dto.getString("GWAEL004"));
        UpdateWrapper<GwaglT> gwaglTUpdateWrapper = new UpdateWrapper<>();
        gwaglTUpdateWrapper.eq("GWAGLENT", shiroUser.getDBC());
        gwaglTUpdateWrapper.eq("GWAGL001", dto.getString("GWAE001"));*/

        gwae.update(gwaeTUpdateWrapper);
        gwael.update(gwaelTUpdateWrapper);
        /*gwbwT.update(gwbwTUpdateWrapper);
        gwbwl.update(gwbwlTUpdateWrapper);
        gwagT.update(gwagTUpdateWrapper);
        gwagl.update(gwaglTUpdateWrapper);*/
    }


    @Transactional
    @Override
    public void delete(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwaeT gwae = new GwaeT();
        UpdateWrapper<GwaeT> gwaeTUpdateWrapper = new UpdateWrapper<>();
        gwaeTUpdateWrapper.eq("GWAEENT", shiroUser.getDBC());
        gwaeTUpdateWrapper.eq("GWAE001", dto.getString("GWAE001"));

        GwaelT gwael = new GwaelT();
        UpdateWrapper<GwaelT> gwaelTUpdateWrapper = new UpdateWrapper<>();
        gwaelTUpdateWrapper.eq("GWAELENT", shiroUser.getDBC());
        gwaelTUpdateWrapper.eq("GWAEL001", dto.getString("GWAE001"));

        /*GwbwT gwbw = new GwbwT();
        UpdateWrapper<GwbwT> gwbwTUpdateWrapper = new UpdateWrapper<>();
        gwbwTUpdateWrapper.eq("GWBWENT", shiroUser.getDBC());
        gwbwTUpdateWrapper.eq("GWBW001", dto.getString("GWAE001"));

        GwbwlT gwbwl = new GwbwlT();
        UpdateWrapper<GwbwlT> gwbwlTUpdateWrapper = new UpdateWrapper<>();
        gwbwlTUpdateWrapper.eq("GWBWLENT", shiroUser.getDBC());
        gwbwlTUpdateWrapper.eq("GWBWL001", dto.getString("GWAE001"));

        GwagT gwag = new GwagT();
        UpdateWrapper<GwagT> gwagTUpdateWrapper = new UpdateWrapper<>();
        gwagTUpdateWrapper.eq("GWAGENT", shiroUser.getDBC());
        gwagTUpdateWrapper.eq("GWAG001", dto.getString("GWAE001"));

        GwaglT gwagl = new GwaglT();
        UpdateWrapper<GwaglT> gwaglTUpdateWrapper = new UpdateWrapper<>();
        gwaglTUpdateWrapper.eq("GWAGLENT", shiroUser.getDBC());
        gwaglTUpdateWrapper.eq("GWAGL001", dto.getString("GWAE001"));*/

        gwae.delete(gwaeTUpdateWrapper);
        gwael.delete(gwaelTUpdateWrapper);
        /*gwbw.delete(gwbwTUpdateWrapper);
        gwbwl.delete(gwbwlTUpdateWrapper);
        gwag.delete(gwagTUpdateWrapper);
        gwagl.delete(gwaglTUpdateWrapper);*/
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwaeT> gwaeTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (GwaeT gwaeT : gwaeTList) {
            UpdateWrapper<GwaeT> gwaeTUpdateWrapper = new UpdateWrapper<>();
            gwaeTUpdateWrapper.eq("GWAEENT", shiroUser.getDBC());
            gwaeTUpdateWrapper.eq("GWAE001", gwaeT.getGWAE001());

            GwaelT gwael = new GwaelT();
            UpdateWrapper<GwaelT> gwaelTUpdateWrapper = new UpdateWrapper<>();
            gwaelTUpdateWrapper.eq("GWAELENT", shiroUser.getDBC());
            gwaelTUpdateWrapper.eq("GWAEL001", gwaeT.getGWAE001());

            /*GwbwT gwbw = new GwbwT();
            UpdateWrapper<GwbwT> gwbwTUpdateWrapper = new UpdateWrapper<>();
            gwbwTUpdateWrapper.eq("GWBWENT", shiroUser.getDBC());
            gwbwTUpdateWrapper.eq("GWBW001", gwaeT.getGWAE001());

            GwbwlT gwbwl = new GwbwlT();
            UpdateWrapper<GwbwlT> gwbwlTUpdateWrapper = new UpdateWrapper<>();
            gwbwlTUpdateWrapper.eq("GWBWLENT", shiroUser.getDBC());
            gwbwlTUpdateWrapper.eq("GWBWL001", gwaeT.getGWAE001());

            GwagT gwag = new GwagT();
            UpdateWrapper<GwagT> gwagTUpdateWrapper = new UpdateWrapper<>();
            gwagTUpdateWrapper.eq("GWAGENT", shiroUser.getDBC());
            gwagTUpdateWrapper.eq("GWAG001", gwaeT.getGWAE001());

            GwaglT gwagl = new GwaglT();
            UpdateWrapper<GwaglT> gwaglTUpdateWrapper = new UpdateWrapper<>();
            gwaglTUpdateWrapper.eq("GWAGLENT", shiroUser.getDBC());
            gwaglTUpdateWrapper.eq("GWAGL001", gwaeT.getGWAE001());*/

            gwaeT.delete(gwaeTUpdateWrapper);
            gwael.delete(gwaelTUpdateWrapper);
            /*gwbw.delete(gwbwTUpdateWrapper);
            gwbwl.delete(gwbwlTUpdateWrapper);
            gwag.delete(gwagTUpdateWrapper);
            gwagl.delete(gwaglTUpdateWrapper);*/
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getlistPage(Page<Object> page, Dto dto) {
        if(dto.getString("hasPermission").equals("true")){
            dto.put("GWAES", sysAuthorityService.getGwaesAuthority(dto));
        }

        List<Map<String, Object>> gwaeDtos = this.baseMapper.getlistPage(page, dto);

        return gwaeDtos;
    }
}
